import java.net.*;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.io.*;
public class TCPclient {

	public static void main (String args[]) {
		// arguments supply hostname and message

		args=new String [] {"7896", "16" , "1"};

		if(args.length>=1){
			int N = Integer.parseInt(args[1]);
			String port=args[0];
			int clients=Integer.parseInt(args[2]);
			for( int numClients=0; numClients<clients; numClients++){
				ClientRequest newCliente = new ClientRequest (port,N,numClients);
				newCliente.start();
			}
		}

		else {
			System.out.println("usage TCPclient [serverPort] [N]");
		}
	}

}
class ClientRequest extends Thread {

	int serverPort;
	String msg;
	Hashtable <String,clientPetition> requestsVector= new Hashtable <String,clientPetition> ();
	String [] operators = {"+", "-", "*","/"}; 
	Hashtable <Integer,Integer> nSized= new Hashtable <Integer,Integer>();


	int [] N = {8,16,32,64,128,256,512,1024,2048,4096}; 
	private BufferedReader in;
	private String data;
	private int selectN;
	private int clientID;
	public ClientRequest (String serverport, int _N, int ClietID){

		this.serverPort=Integer.parseInt(serverport);
		this.selectN=_N;
		this.clientID=ClietID;

	}


	public void run (){
		Socket s = null;
		try{
			//int serverPort = 7896;
			s = new Socket("localhost", this.serverPort); 

			//	tStream in = new DataInputStream( s.getInputStream());
			in = new BufferedReader( new InputStreamReader(s.getInputStream()));
			DataOutputStream out =new DataOutputStream( s.getOutputStream());
			String requesID="";
			for(int i=0; i<4; i++){
				requesID=this.selectN +"_"+operators[i];
				clientPetition request=new clientPetition (requesID);
				out.writeUTF(requesID);
				requestsVector.put(requesID,request);
			}

			for(int i=0; i<4; i++){
				this.data = this.in.readLine();
				String requestID=this.data;
				int cont=0;
				this.data = this.data.concat("\n" + this.in.readLine());
				this.data = this.data.concat("\n" + this.in.readLine());
				while(cont <= this.selectN){
					this.data = this.data.concat("\n" + this.in.readLine());
					cont++;
				}
				



				clientPetition requestReply=requestsVector.remove(requestID);
				requestReply.setEndTime(System.currentTimeMillis());
				requestReply.setMatrixR(this.data);

				requestsVector.put(requestReply.getRequestID(),requestReply);
			}

			System.out.println("C" + this.clientID + " : RWtime--Ptime--Latency");
		

			String key;
			Set<String> set = (Set<String>) requestsVector.keySet();
			Iterator <String> itr=set.iterator();
			while (itr.hasNext()) {
				key = itr.next();
				System.out.println(requestsVector.get(key).getRequestID() + " : " + requestsVector.get(key).getRWtime() + "--" + requestsVector.get(key).getPtime() + "--" + requestsVector.get(key).getLtime());
// uncomment to print matrix
				//System.out.println(requestsVector.get(key).getMatrixR());
			}

		}catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage());
		}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
		}catch (IOException e){System.out.println("readline:"+e.getMessage());
		}finally {if(s!=null) try {s.close();}catch (IOException e){System.out.println("close:"+e.getMessage());}}

	}

}
